/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.action;

import cn.tom.kit.StringUtil;
import com.cunyu.bean.Form;
import com.cunyu.service.BarCodeService;
import com.cunyu.service.TagService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 其他功能
 * @author win
 * 2019/04/24-14:17
 */
@Controller
public class OtherController extends BaseController{

    @Resource
    TagService tagService ;

    @Resource
    BarCodeService barCodeService ;

    @RequestMapping("/other/tags")
    public String tags(Model model,String tag){
        List<Map<String, Object>> tags = tagService.getTags();
        model.addAttribute("respData",tags) ;
        model.addAttribute("tag",tag) ;
        return "other/tags" ;
    }

    @RequestMapping("/other/tagInfo")
    public void tagInfo(HttpServletResponse response, Form form) throws IOException {
        int tag ;
        if(StringUtil.hasLength(form.getId())){
            tag = Integer.valueOf(form.getId());
            tagService.updataTag(form) ;
        }else {
            tag = tagService.newTag(form) ;
        }

        response.sendRedirect("/other/tags?tag="+tag);
    }

    @RequestMapping("/group")
    public String group(Model model,String qcode) {
        String url = barCodeService.groupUrl(qcode);
        model.addAttribute("respData",url) ;
        return "add";
    }

    @RequestMapping("/other/group")
    public String wxGroup(Model model) {
        Map<String, Object> respData = barCodeService.useful();
        model.addAttribute("respData",respData) ;
        return "other/gm";
    }

    @RequestMapping("/other/groupInfo")
    public void groupInfo(HttpServletResponse res ,String img) throws IOException {
        barCodeService.addUrl(img,"");
        Map<String, Object> respData = barCodeService.useful();
        res.sendRedirect("/other/group");
    }

    // ajax data info

    @RequestMapping("/other/tag")
    public void tag(HttpServletResponse response,String id){
        Map<String, Object> tag = tagService.getTag(Integer.valueOf(id));
        printOK(response,"success",tag);
    }

}
